package cn.com.elite;

import java.sql.Driver;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import cn.com.elite.util.Util;

public class StartThread extends HttpServlet{
	private static final Log log=LogFactory.getLog(StartThread.class);
	private String initialSize;  //初始化连接数
	private String minIdle;  //最小空闲连接
	private String maxIdle;   //最大空闲连接
	private String maxWait;   //超时回收时间(以毫秒为单位)
	private String maxActive;  //最大连接数
	private String removeAbandonedTimeout;  //超时时间(以秒数为单位)
	private boolean removeAbandoned=true;  //是否自动回收超时连接
	private boolean logAbandoned =true;  //是否在自动回收超时连接的时候打印连接的超时错误
	private String driveClassName;
	public static List sql_list;
	public void init(){
		try{
			log.info("--------------------------StartThread-------------------------------------");
			sql_list=new ArrayList();
			String Hz=this.getInitParameter("Hz");
			int HZ1=Integer.parseInt(Hz);
			String sql1=this.getInitParameter("sql1");
			String sql2=this.getInitParameter("sql2");
			String sql3=this.getInitParameter("sql3");
			initialSize=this.getInitParameter("initialSize");
			minIdle=this.getInitParameter("minIdle");
			maxIdle=this.getInitParameter("maxIdle");
			maxWait=this.getInitParameter("maxWait");
			maxActive=this.getInitParameter("maxActive");
			removeAbandonedTimeout=this.getInitParameter("removeAbandonedTimeout");
			driveClassName=this.getInitParameter("driveClassName");
			getjdbclist(sql1);
			EliteTread.setSql2(sql2);
			EliteTread.setSql3(sql3);
	        EliteTread.setHz(HZ1);
	        EliteTread td=new EliteTread();
			Thread t = new Thread(td); 
	        t.start(); //   启动任务
		}catch(Exception e){
			log.error("init()错误:"+Util.getERRO(e)); 
		}
	}
	private void getjdbclist(String sql1)throws Exception{
		try{
			cn.com.elite.EliteBean ebean=getbean();      
	        List sql_list1=ebean.chaxun_sql(sql1);
	        if(sql_list1!=null&&sql_list1.size()>0){
	        	for(int i=0;i<sql_list1.size();i++){
	        		Map map=(Map)sql_list1.get(i);
	        		String DBUSER=(String)map.get("DBUSER");
	        		String DBPASSWORD=(String)map.get("DBPASSWORD");
	        		String JDBC_URL=(String)map.get("JDBC_URL");
	        		String PROJECT_ID=(String)map.get("PROJECT_ID");
	        		Map map1=new HashMap();
	        		map1.put("dataSource",getJDBC(JDBC_URL,DBUSER,DBPASSWORD));
	        		map1.put("PROJECT_ID",PROJECT_ID);
	        		sql_list.add(map1);
	        	}
	        }
		}catch(Exception e){
			log.error("getjdbclist:"+Util.getERRO(e));
		}
	}
	private cn.com.elite.EliteBean getbean()throws Exception{
		ServletContext application;   
        WebApplicationContext wac;   
        application = getServletContext();   
        wac=WebApplicationContextUtils.getWebApplicationContext(application);//获取spring的context  
        cn.com.elite.EliteBean ebean=(cn.com.elite.EliteBean)wac.getBean("ebean"); 
        return ebean;
	}
	private BasicDataSource getJDBC(String url,String username,String password)throws Exception{
    	BasicDataSource dataSource=new BasicDataSource();
        dataSource.setDriverClassName(driveClassName);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        //初始化连接数
        dataSource.setInitialSize(Integer.parseInt(initialSize));        
        //最小空闲连接
        dataSource.setMinIdle(Integer.parseInt(minIdle));
        //最大空闲连接
        dataSource.setMaxIdle(Integer.parseInt(maxIdle));
        //超时回收时间(以毫秒为单位)
        dataSource.setMaxWait(Long.parseLong(maxWait));       
        //最大连接数
        dataSource.setMaxActive(Integer.parseInt(maxActive));
        dataSource.setRemoveAbandoned(removeAbandoned);
        dataSource.setRemoveAbandonedTimeout(Integer.parseInt(removeAbandonedTimeout));
        dataSource.setLogAbandoned(logAbandoned);
        return dataSource;
	}

}